package net.goutalk.glcs.module.language.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.module.language.dto.LanguageTypeListDto;
import net.goutalk.glcs.module.language.entity.LanguageType;
import net.goutalk.glcs.module.language.mapper.LanguageTypeMapper;
import net.goutalk.glcs.module.language.service.ILanguageTypeService;
import net.goutalk.glcs.module.language.vo.LanguageTypeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 多语言语言类型表 服务实现类
 * </p>
 *
 * @author zlf
 * @since 2022-06-30
 */
@Service
public class LanguageTypeServiceImpl extends ServiceImpl<LanguageTypeMapper, LanguageType> implements ILanguageTypeService {

    @Override
    public boolean setMainLanguage(Long id) {
        QueryWrapper<LanguageType> query = new QueryWrapper<>();
        query.lambda().eq(LanguageType::getIsMain, 1);
        LanguageType oldGlcsLgType = baseMapper.selectOne(query);
        if (oldGlcsLgType != null) {
            oldGlcsLgType.setIsMain(0);
            // 更新
            retBool(baseMapper.updateById(oldGlcsLgType));
        }
        LanguageType glcsLgType = baseMapper.selectById(id);
        if (glcsLgType != null) {
            glcsLgType.setIsMain(1);
            return retBool(baseMapper.updateById(glcsLgType));
        }
        return false;
    }

    @Override
    public PageOutput<LanguageTypeVo> getPageData(LanguageTypeListDto dto) {
        String keyword = dto.getKeyword();
        IPage<LanguageType> page = ConventPage.getPage(dto);
        this.page(page, Wrappers.<LanguageType>query().lambda()
                .like(!StringUtils.isEmpty(keyword), LanguageType::getName, keyword)
                .or().like(!StringUtils.isEmpty(keyword), LanguageType::getCode, keyword));
        return ConventPage.getPageOutput(page, LanguageTypeVo.class);
    }

    @Override
    public List<LanguageType> getDataList(String keyword) {
        return this.list(Wrappers.<LanguageType>query()
                .lambda().like(!StringUtils.isEmpty(keyword), LanguageType::getName, keyword)
                .like(!StringUtils.isEmpty(keyword), LanguageType::getName, keyword)
                .orderByAsc(LanguageType::getName));
    }
}
